package cm.iccsoft.android.telecomcost.activities;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.Utility;
import cm.iccsoft.android.telecomcost.adapter.ZoneAdapter;
import cm.iccsoft.android.telecomcost.entities.Zone;
import cm.iccsoft.android.telecomcost.ui.ZoneDialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ZonesListActivity extends TelecomcostActivity {

	private ListView list;
	private DatabaseManager db;
	public static final int DIALOG_NEWZONE_ID = 1;
	public static final int DIALOG_EDITZONE_ID = 2;
	
	public List<Zone> zones;

	@Override
	protected Dialog onCreateDialog(int id) {
		Log.i("ZonesListActivity","Creating dialog ohne bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_NEWZONE_ID:
			dialog = new ZoneDialog(ZonesListActivity.this, null);
			break;
		default:
			dialog = null;
		}
		return dialog;
	}
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle data) {
		Log.i("ZonesListActivity","Creating dialog with bundle");
		Dialog dialog = null;
		Gson gson = TelecomCost.getGson();
		switch (id) {		
		case DIALOG_EDITZONE_ID:
			dialog = new ZoneDialog(ZonesListActivity.this,data.getString("zone") != null ? gson.fromJson(data.getString("zone"),Zone.class) : null);
			break;
		default:
			dialog = null;
		}
		
		if(dialog == null)
		{
			return onCreateDialog(id);
		}
		
		return dialog;
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		Log.i("ProviderListActivity","Preparing dialog with bundle");
		super.onPrepareDialog(id, dialog, args);
		Utility.setDialogLayout(dialog);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		Log.i("ProviderListActivity","Preparing dialog ohne bundle");
		super.onPrepareDialog(id, dialog);
		Utility.setDialogLayout(dialog);
	}	

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("ZonesListActivity", "Creating the activity " + this.getTitle()
				+ "...setup of global state of the activity");
		
		super.onCreate(savedInstanceState);
		db = TelecomCost.getDatabaseManager();		

		setContentView(R.layout.zones_list);

		list = (ListView) findViewById(R.id.listViewZones);		

		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				showSelectedZone(arg2);
			}
		});				

		bindZones();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("ZonesListActivity","creating options menu for zones : ");
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_zones, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		
	    switch (item.getItemId()) {
	    case R.id.menu_item_new_zone:	    	
	    	showZoneDialog();	    	
	        return true;	        
	    case R.id.menu_item_delete_zones:	    	
	    	deleteZones();
			bindZones();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

	protected void deleteZones() {						
		Log.i("ZonesListActivity","Deleting selected zones : ");
		int i = 0;	
		DatabaseManager db = TelecomCost.getDatabaseManager();
		
		while(i < zones.size())
		{
			Zone zone = zones.get(i);
			if(zone.isSelected())
			{				
				db.deleteZone(zone);				
			}			
			i++;
		}					
	}

	protected void showSelectedZone(int index) {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		Log.i("ZonesListActivity", "Destroying the activity " + this.getTitle());
		db = null;
		list = null;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		Log.i("ZonesListActivity", "Pausing the activity " + this.getTitle());
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		Log.i("ZonesListActivity",
				"Postcreating the activity " + this.getTitle());
	}

	@Override
	protected void onPostResume() {
		// TODO Auto-generated method stub
		super.onPostResume();
		Log.i("ZonesListActivity",
				"PostResuming the activity " + this.getTitle());
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.i("ZonesListActivity", "Restarting the activity " + this.getTitle());
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		Log.i("ZonesListActivity", "onRestoreInstanceState the activity "
				+ this.getTitle());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("ZonesListActivity", "Resuming the activity " + this.getTitle());
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		Log.i("ZonesListActivity",
				"onSaveInstanceState the activity " + this.getTitle());
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("ZonesListActivity", "Starting the activity " + this.getTitle());
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i("ZonesListActivity", "Stopping the activity " + this.getTitle());
	}

	private void showZoneDialog() {	
		Log.i("ZonesListActivity","displaying edit zone dialog : ");
		showDialog(DIALOG_NEWZONE_ID);
	}

	public void bindZones() {	
		Log.i("ZonesListActivity","binding zones");
		zones = db.getZones();		

		list.setAdapter(new ZoneAdapter(ZonesListActivity.this,
				R.layout.item_row, (ArrayList<Zone>) zones,this));
	}

	@Override
	public String toString() {		
		return "ZonesListActivity";
	}
	
	public Zone getZone(String name)
	{
		String _name;
		int i = 0;
		while(i < zones.size())
		{		
			_name = zones.get(i).getName();
			if(_name.equals(name))
			{
				Log.i("ZonesListActivity","this zone already exists...we update it");
				return zones.get(i);
			}			
			i++;
		}
		
		return null;		
	}
}
